package com.darrenchan.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-28 18:45
 */
public class Q590 {
    public List<Integer> postorder(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }

        Stack<Node> stack1 = new Stack<>();
        Stack<Node> stack2 = new Stack<>();

        stack1.add(root);
        while (!stack1.isEmpty()) {
            root = stack1.pop();
            stack2.add(root);

            for (Node node : root.children) {
                if (node != null) {
                    stack1.add(node);
                }
            }

        }

        while (!stack2.isEmpty()) {
            list.add(stack2.pop().val);
        }

        return list;
    }
}
